<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>认证Authentication | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../03-day/other/quan-xian-permissions.html" />
    
    
    <link rel="prev" href="../../03-day/other.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="4.1"
        data-chapter-title="认证Authentication"
        data-filepath="03-day/other/authentication.md"
        data-basepath="../.."
        data-revision="Sun May 10 2020 14:49:33 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Django REST framework
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01-day.html">
            
                
                    <a href="../../01-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django REST framework流程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01-day/restful.html">
            
                
                    <a href="../../01-day/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        RESTfull
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="01-day/restful/mode.html">
            
                
                    <a href="../../01-day/restful/mode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="01-day/restful/restful.html">
            
                
                    <a href="../../01-day/restful/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="01-day/restful/method.html">
            
                
                    <a href="../../01-day/restful/method.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.</b>
                        
                        RESTful设计思想
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="01-day/restful/method/request.html">
            
                
                    <a href="../../01-day/restful/method/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.1.</b>
                        
                        请求相关
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="01-day/restful/method/response.html">
            
                
                    <a href="../../01-day/restful/method/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.2.</b>
                        
                        响应相关
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="01-day/restful/prepare.html">
            
                
                    <a href="../../01-day/restful/prepare.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.4.</b>
                        
                        项目准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="01-day/restful/django.html">
            
                
                    <a href="../../01-day/restful/django.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.5.</b>
                        
                        使用Django开发REST 接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="01-day/restful/task.html">
            
                
                    <a href="../../01-day/restful/task.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.6.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01-day/restful/djangorestful-framework.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Django REST framework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01-day/restful/djangorestful-framework/config.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework/config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="01-day/restful/djangorestful-framework/like.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework/like.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.2.</b>
                        
                        Django REST framework魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02-day.html">
            
                
                    <a href="../../02-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        序列化和反序列化
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="02-day/modify.html">
            
                
                    <a href="../../02-day/modify.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Serializer字段和选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="02-day/serializer.html">
            
                
                    <a href="../../02-day/serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="02-day/serializer/base.html">
            
                
                    <a href="../../02-day/serializer/base.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.1.</b>
                        
                        基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="02-day/serializer/assoc.html">
            
                
                    <a href="../../02-day/serializer/assoc.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.2.</b>
                        
                        关联对象嵌套序列化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="02-day/un-serializer.html">
            
                
                    <a href="../../02-day/un-serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        反序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="02-day/un-serializer/validate.html">
            
                
                    <a href="../../02-day/un-serializer/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.</b>
                        
                        验证
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1.1" data-path="02-day/un-serializer/validate/type.html">
            
                
                    <a href="../../02-day/un-serializer/validate/type.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.1.</b>
                        
                        字段类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.2" data-path="02-day/un-serializer/validate/field.html">
            
                
                    <a href="../../02-day/un-serializer/validate/field.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.2.</b>
                        
                        字段选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.3" data-path="02-day/un-serializer/validate/validatefieldname.html">
            
                
                    <a href="../../02-day/un-serializer/validate/validatefieldname.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.3.</b>
                        
                        validate\_fieldname
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.4" data-path="02-day/un-serializer/validate/validate.html">
            
                
                    <a href="../../02-day/un-serializer/validate/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.4.</b>
                        
                        validate
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="02-day/un-serializer/save.html">
            
                
                    <a href="../../02-day/un-serializer/save.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.2.</b>
                        
                        保存
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="02-day/modelserializer.html">
            
                
                    <a href="../../02-day/modelserializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03-day/view.html">
            
                
                    <a href="../../03-day/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03-day/view/requestresponse.html">
            
                
                    <a href="../../03-day/view/requestresponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Request 与 Response
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="03-day/view/requestresponse/request.html">
            
                
                    <a href="../../03-day/view/requestresponse/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.1.</b>
                        
                        Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="03-day/view/requestresponse/response.html">
            
                
                    <a href="../../03-day/view/requestresponse/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.2.</b>
                        
                        Response
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03-day/view/view.html">
            
                
                    <a href="../../03-day/view/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="03-day/view/view/apiview.html">
            
                
                    <a href="../../03-day/view/view/apiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.1.</b>
                        
                        APIView\(一级\)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="03-day/view/view/genericapiview.html">
            
                
                    <a href="../../03-day/view/view/genericapiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.2.</b>
                        
                        GenericAPIView\(二级\)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="03-day/view/view/mixin.html">
            
                
                    <a href="../../03-day/view/view/mixin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.3.</b>
                        
                        Mixin
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="03-day/view/view/subclass.html">
            
                
                    <a href="../../03-day/view/view/subclass.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.4.</b>
                        
                        子类视图\(三级\)
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03-day/view/viewset.html">
            
                
                    <a href="../../03-day/view/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        视图集ViewSet
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="03-day/view/viewset/viewset.html">
            
                
                    <a href="../../03-day/view/viewset/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="03-day/view/viewset/genericviewset.html">
            
                
                    <a href="../../03-day/view/viewset/genericviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GenericViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="03-day/view/viewset/modelviewset.html">
            
                
                    <a href="../../03-day/view/viewset/modelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        ModelViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="03-day/view/viewset/readonlymodelviewset.html">
            
                
                    <a href="../../03-day/view/viewset/readonlymodelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        ReadOnlyModelViewSet
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="03-day/view/router.html">
            
                
                    <a href="../../03-day/view/router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="03-day/other.html">
            
                
                    <a href="../../03-day/other.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        高级功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="4.1" data-path="03-day/other/authentication.html">
            
                
                    <a href="../../03-day/other/authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        认证Authentication
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="03-day/other/quan-xian-permissions.html">
            
                
                    <a href="../../03-day/other/quan-xian-permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        权限Permissions
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="03-day/other/throttling.html">
            
                
                    <a href="../../03-day/other/throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        限流Throttling
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="03-day/other/filtering.html">
            
                
                    <a href="../../03-day/other/filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        过滤Filtering
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="03-day/other/order.html">
            
                
                    <a href="../../03-day/other/order.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        排序OrderingFilter
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="03-day/other/pagination.html">
            
                
                    <a href="../../03-day/other/pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        分页Pagination
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="03-day/other/exceptions.html">
            
                
                    <a href="../../03-day/other/exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        异常处理 Exceptions
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x8BA4;&#x8BC1;authentication">&#x8BA4;&#x8BC1;Authentication </h1>
<p><a href="#">&#x8BA4;&#x8BC1;</a></p>
<h2 id="1&#x6982;&#x5FF5;">1.&#x6982;&#x5FF5;</h2>
<p><img src="../../assets/&#x8EAB;&#x4EFD;.png" alt=""></p>
<p>&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x662F;&#x5C06;&#x4F20;&#x5165;&#x8BF7;&#x6C42;&#x4E0E;&#x4E00;&#x7EC4;&#x6807;&#x8BC6;&#x51ED;&#x636E;&#xFF08;&#x4F8B;&#x5982;&#xFF0C;&#x8BF7;&#x6C42;&#x6765;&#x81EA;&#x7684;&#x7528;&#x6237;&#x6216;&#x4E0E;&#x5176;&#x8FDB;&#x884C;&#x7B7E;&#x540D;&#x7684;&#x4EE4;&#x724C;&#xFF09;&#x76F8;&#x5173;&#x8054;&#x7684;&#x673A;&#x5236;&#x3002;&#x7136;&#x540E;&#xFF0C;<a href="https://www.django-rest-framework.org/api-guide/permissions/" target="_blank">&#x6743;&#x9650;</a>&#x548C;<a href="https://www.django-rest-framework.org/api-guide/throttling/" target="_blank">&#x9650;&#x5236;</a>&#x7B56;&#x7565;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x51ED;&#x636E;&#x6765;&#x786E;&#x5B9A;&#x662F;&#x5426;&#x5E94;&#x5141;&#x8BB8;&#x8BE5;&#x8BF7;&#x6C42;&#x3002;</p>
<p><strong>&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x672C;&#x8EAB;&#x4E0D;&#x4F1A;&#x5141;&#x8BB8;&#x6216;&#x4E0D;&#x5141;&#x8BB8;&#x4F20;&#x5165;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x5B83;&#x53EA;&#x4F1A;&#x6807;&#x8BC6;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#x7684;&#x51ED;&#x636E;&#x3002;</strong></p>
<p><strong>&#x8BA4;&#x8BC1;&#x7BA1;&#x7406;&#x4E00;&#x822C;&#x548C;&#x6743;&#x9650;&#x7BA1;&#x7406;&#x914D;&#x5408;&#x4F7F;&#x7528;&#x3002;</strong></p>
<h2 id="2&#x8BA4;&#x8BC1;&#x65B9;&#x5F0F;">2.&#x8BA4;&#x8BC1;&#x65B9;&#x5F0F;</h2>
<ul>
<li><h4 id="&#x57FA;&#x672C;&#x8BA4;&#x8BC1;&#xFF08;basicauthentication&#xFF09;"><a href="https://www.django-rest-framework.org/api-guide/authentication/#basicauthentication" target="_blank">&#x57FA;&#x672C;&#x8BA4;&#x8BC1;</a>&#xFF08;BasicAuthentication&#xFF09;</h4>
</li>
</ul>
<p>&#x6B64;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x65B9;&#x6848;&#x4F7F;&#x7528;<a href="https://tools.ietf.org/html/rfc2617" target="_blank">HTTP&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;</a>&#xFF0C;&#x8BE5;&#x8EAB;&#x4EFD;&#x9488;&#x5BF9;&#x7528;&#x6237;&#x7684;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#x8FDB;&#x884C;&#x4E86;&#x7B7E;&#x540D;&#x3002;<strong>&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x901A;&#x5E38;&#x4EC5;&#x9002;&#x7528;&#x4E8E;&#x6D4B;&#x8BD5;</strong>&#x3002;</p>
<p><img src="../../assets/&#x57FA;&#x672C;&#x8BA4;&#x8BC1;.png" alt=""></p>
<p>&#x5982;&#x679C;&#x6210;&#x529F;&#x901A;&#x8FC7;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;<code>request.user</code>&#x5C06;&#x662F;Django<code>User</code>&#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#x672A;&#x7ECF;&#x6388;&#x6743;&#x7684;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x7684;&#x54CD;&#x5E94;&#x5C06;&#x88AB;&#x62D2;&#x7EDD; <code>HTTP 401 Unauthorized</code></p>
<p><img src="../../assets/&#x57FA;&#x672C;&#x8BA4;&#x8BC1;&#x672A;&#x901A;&#x8FC7;.png" alt=""></p>
<ul>
<li><h4 id="&#x4F1A;&#x8BDD;&#x8BA4;&#x8BC1;&#xFF08;sessionauthentication&#xFF09;"><a href="https://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication" target="_blank">&#x4F1A;&#x8BDD;&#x8BA4;&#x8BC1;</a>&#xFF08;SessionAuthentication&#xFF09;</h4>
</li>
</ul>
<p>&#x6B64;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x65B9;&#x6848;&#x4F7F;&#x7528;Django&#x7684;&#x9ED8;&#x8BA4;&#x4F1A;&#x8BDD;&#x540E;&#x7AEF;&#x8FDB;&#x884C;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x3002;<strong>&#x4F1A;&#x8BDD;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x9002;&#x7528;&#x4E8E;&#x5728;&#x4E0E;&#x60A8;&#x7684;&#x7F51;&#x7AD9;&#x76F8;&#x540C;&#x7684;&#x4F1A;&#x8BDD;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x8FD0;&#x884C;&#x7684;AJAX&#x5BA2;&#x6237;&#x7AEF;</strong>&#x3002;</p>
<p>&#x5982;&#x679C;&#x6210;&#x529F;&#x901A;&#x8FC7;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;<code>request.user</code>&#x5C06;&#x662F;Django<code>User</code>&#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#x672A;&#x7ECF;&#x6388;&#x6743;&#x7684;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x7684;&#x54CD;&#x5E94;&#x5C06;&#x88AB;&#x62D2;&#x7EDD;<code>HTTP 403 Forbidden</code>&#x3002;</p>
<ul>
<li><h4 id="&#x4EE4;&#x724C;&#x8BA4;&#x8BC1;&#xFF08;tokenauthentication&#xFF09;"><a href="https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication" target="_blank">&#x4EE4;&#x724C;&#x8BA4;&#x8BC1;</a>&#xFF08;TokenAuthentication&#xFF09;</h4>
</li>
</ul>
<p>&#x6B64;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x65B9;&#x6848;&#x4F7F;&#x7528;&#x7B80;&#x5355;&#x7684;&#x57FA;&#x4E8E;&#x4EE4;&#x724C;&#x7684;HTTP&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x65B9;&#x6848;&#x3002;<strong>&#x4EE4;&#x724C;&#x8BA4;&#x8BC1;&#x9002;&#x7528;&#x4E8E;&#x5BA2;&#x6237;&#x7AEF;-&#x670D;&#x52A1;&#x5668;&#x8BBE;&#x7F6E;&#xFF0C;&#x4F8B;&#x5982;&#x672C;&#x673A;&#x53F0;&#x5F0F;&#x673A;&#x548C;&#x79FB;&#x52A8;&#x5BA2;&#x6237;&#x7AEF;</strong>&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x4F7F;&#x5BA2;&#x6237;&#x7AEF;&#x8FDB;&#x884C;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#xFF0C;&#x4EE4;&#x724C;&#x5BC6;&#x94A5;&#x5E94;&#x5305;&#x542B;&#x5728;<code>Authorization</code>HTTP&#x6807;&#x5934;&#x4E2D;&#x3002;&#x5BC6;&#x94A5;&#x5E94;&#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x6587;&#x5B57;&#x201C; Token&#x201D;&#x4F5C;&#x4E3A;&#x524D;&#x7F00;&#xFF0C;&#x5E76;&#x7528;&#x7A7A;&#x683C;&#x5206;&#x9694;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code>Authorization:Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b
</code></pre><blockquote>
<p>&#x8BA4;&#x8BC1;&#x5931;&#x8D25;&#x4F1A;&#x6709;&#x4E24;&#x79CD;&#x53EF;&#x80FD;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF1A;</p>
<ul>
<li>401 Unauthorized &#x672A;&#x8BA4;&#x8BC1;</li>
<li>403 Permission Denied &#x6743;&#x9650;&#x88AB;&#x7981;&#x6B62;</li>
</ul>
</blockquote>
<h2 id="3&#x8BA4;&#x8BC1;&#x5168;&#x5C40;&#x8BBE;&#x7F6E;">3.&#x8BA4;&#x8BC1;&#x5168;&#x5C40;&#x8BBE;&#x7F6E;</h2>
<p>&#x53EF;&#x4EE5;&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x914D;&#x7F6E;&#x5168;&#x5C40;&#x9ED8;&#x8BA4;&#x7684;&#x8BA4;&#x8BC1;&#x65B9;&#x6848;</p>
<pre><code class="lang-py">REST_FRAMEWORK = {
    <span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x7684;&#x8BA4;&#x8BC1;&#x5217;&#x8868;&#xFF1A; session</span>
    <span class="hljs-string">&apos;DEFAULT_AUTHENTICATION_CLASSES&apos;</span>: (
        <span class="hljs-string">&apos;rest_framework.authentication.SessionAuthentication&apos;</span>,  <span class="hljs-comment"># session&#x8BA4;&#x8BC1;</span>
    )
}
</code></pre>
<p>&#x56E0;&#x4E3A; &#x8BA4;&#x8BC1;&#x4E00;&#x822C;&#x90FD;&#x662F;&#x548C;&#x6743;&#x9650;&#x914D;&#x5408;&#x4F7F;&#x7528;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x4E0D;&#x8BBE;&#x7F6E;&#x6743;&#x9650;&#x65F6;&#xFF0C;&#x662F;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x6548;&#x679C;&#x7684;&#x3002;&#x4E3A;&#x4E86;&#x8BA9;&#x5927;&#x5BB6;&#x770B;&#x5230;&#x6700;&#x4F73;&#x6548;&#x679C;&#xFF0C;&#x6211;&#x4EEC;&#x989D;&#x5916;&#x6DFB;&#x52A0;&#x4E00;&#x6761;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x3002;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x7684;&#x610F;&#x601D;&#x662F; &#x53EA;&#x6709;&#x8BA4;&#x8BC1;&#x767B;&#x5F55;&#x7528;&#x6237;&#x624D;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x89C6;&#x56FE;</p>
<pre><code class="lang-py">REST_FRAMEWORK = {
    <span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x7684;&#x8BA4;&#x8BC1;&#x5217;&#x8868;&#xFF1A; session</span>
    <span class="hljs-string">&apos;DEFAULT_AUTHENTICATION_CLASSES&apos;</span>: [
        <span class="hljs-string">&apos;rest_framework.authentication.SessionAuthentication&apos;</span>,
    ],
    <span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x7684;&#x6743;&#x9650;&#x5217;&#x8868;&#xFF1A; &#x53EA;&#x6709;&#x767B;&#x5F55;&#x7528;&#x6237;&#x624D;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;</span>
    <span class="hljs-string">&apos;DEFAULT_PERMISSION_CLASSES&apos;</span>: (
        <span class="hljs-string">&apos;rest_framework.permissions.IsAuthenticated&apos;</span>,
    )
}
</code></pre>
<p>&#x6DFB;&#x52A0;&#x4E86;&#x6743;&#x9650;&#x914D;&#x7F6E;&#x4E4B;&#x540E;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x5982;&#x4E0B;&#x54CD;&#x5E94;</p>
<p><img src="../../assets/403&#x7981;&#x6B62;&#x8BBF;&#x95EE;.png" alt=""></p>
<blockquote>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8D85;&#x7EA7;&#x7BA1;&#x7406;&#x5458;&#xFF0C;&#x767B;&#x5F55;&#x540E;&#x53F0;&#xFF0C;&#x6765;&#x521B;&#x5EFA;session&#x4FE1;&#x606F;</p>
<p>1.&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8D85;&#x7EA7;&#x7528;&#x6237;</p>
<p><img src="../../assets/&#x521B;&#x5EFA;&#x8D85;&#x7EA7;&#x7BA1;&#x7406;&#x5458;.png" alt=""></p>
<p>2.&#x767B;&#x5F55;&#x540E;&#x53F0; <a href="#">http://127.0.0.1:8000/admin/login/?next=/admin/</a></p>
<p><img src="../../assets/&#x767B;&#x5F55;admin&#x540E;&#x53F0;.png" alt="">3.&#x767B;&#x5F55;&#x6210;&#x529F;&#x751F;&#x6210;session</p>
<p><img src="../../assets/&#x767B;&#x5F55;&#x6210;&#x529F;&#x751F;&#x6210;session.png" alt=""></p>
</blockquote>
<p>&#x767B;&#x5F55;&#x6210;&#x529F;&#x4E4B;&#x540E;&#xFF0C;&#x518D;&#x6B21;&#x5237;&#x65B0;&#x9875;&#x9762;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x4E86;</p>
<p><img src="../../assets/&#x62E5;&#x6709;session&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;.png" alt=""></p>
<h2 id="4&#x8BA4;&#x8BC1;&#x6307;&#x5B9A;&#x89C6;&#x56FE;&#x8BBE;&#x7F6E;">4.&#x8BA4;&#x8BC1;&#x6307;&#x5B9A;&#x89C6;&#x56FE;&#x8BBE;&#x7F6E;</h2>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x6BCF;&#x4E2A;&#x89C6;&#x56FE;&#x4E2D;&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;authentication_classess&#x5C5E;&#x6027;&#x6765;&#x8BBE;&#x7F6E;</p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> book.models <span class="hljs-keyword">import</span> BookInfo
<span class="hljs-keyword">from</span> book.serializers <span class="hljs-keyword">import</span> BookInfoModelSerializer
<span class="hljs-keyword">from</span> rest_framework.viewsets <span class="hljs-keyword">import</span> ModelViewSet
<span class="hljs-keyword">from</span> rest_framework.authentication <span class="hljs-keyword">import</span> TokenAuthentication


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookModelViewSet</span><span class="hljs-params">(ModelViewSet)</span>:</span>
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoModelSerializer

    <span class="hljs-comment">#&#x5355;&#x4E2A;&#x89C6;&#x56FE;&#xFF0C;&#x8BBE;&#x7F6E;&#x5355;&#x72EC;&#x8BA4;&#x8BC1;&#x65B9;&#x5F0F;</span>
    authentication_classes = [TokenAuthentication]
</code></pre>
<p><img src="../../assets/&#x89C6;&#x56FE;&#x7684;&#x914D;&#x7F6E;&#x4F18;&#x5148;&#x7EA7;&#x5927;&#x4E8E;settings.png" alt=""></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../03-day/other.html" class="navigation navigation-prev " aria-label="Previous page: 高级功能"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../03-day/other/quan-xian-permissions.html" class="navigation navigation-next " aria-label="Next page: 权限Permissions"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/fontsettings.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
